package linc.cool.framework.protocol.dubbo;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import linc.cool.framework.Invocation;

import java.util.concurrent.Callable;

/**
 * @author yqlin
 * @date 2022/3/11 4:48 下午
 * @description
 */
public class NettyClientHandler extends SimpleChannelInboundHandler<String> implements Callable<String> {
    private ChannelHandlerContext ctx;
    private Invocation invocation;
    private String msg;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        this.ctx = ctx;
    }

    @Override
    protected synchronized void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        this.msg = msg;
        notify();
    }

    @Override
    public synchronized String call() throws Exception {
        this.ctx.writeAndFlush(this.invocation);
        wait();
        return this.msg;

    }

    public void setInvocation(Invocation invocation) {
        this.invocation = invocation;
    }
}

